package edu.sias.userQuestion.controller;

import edu.sias.common.core.controller.BaseController;
import edu.sias.common.core.domain.AjaxResult;
import edu.sias.userQuestion.domain.Userques;
import edu.sias.userQuestion.service.UserquestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 用户和题目关联模块
 */
@RestController
@RequestMapping("/user_question")
public class UserquestionController extends BaseController {

    @Autowired
    private UserquestionService userquestionService;
    //思路制作一个es然后用户点击提交题目的时候，会把用户的答案存在里面，并在数据库里面生成一条做题记录
    //当用户在点击这个题会发生查询到这个题自己的答案
    //当用户点击提交题目，会添加

    /**
     * 用户点击提交
     * @return
     */
    @PostMapping("/add.do")
    public AjaxResult add(@RequestBody Userques userQues, HttpSession  session) throws IOException {
//        userQues.setUser_id(Integer.parseInt(getUserInfoFromSession(session).getUserId()));

        userquestionService.add(userQues, session);
        return AjaxResult.success("提交成功");
    }

    /**
     * 用户在次查看自己的答案
     * @return
     */
    @GetMapping("/findById.do")
    public AjaxResult findById(Integer questionId, HttpSession session) throws IOException {
        Userques userques = new Userques();
        userques.setUser_id(Integer.parseInt(getUserInfoFromSession(session).getUserId()));
        return AjaxResult.success(userquestionService.findById(userques));
    }

}
